package com.guet;

import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.List;

public class fileUse {
    private Object[] startFileInputBuf(String path) {
        File file = new File(path);
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader bfr = null;
        try {
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
            bfr = new BufferedReader(isr);
        } catch (IOException e) {
            System.out.println("文件加载错误");
        }
        Object[] objs={(Object)fis,(Object)isr,(Object)bfr};
        return objs;
    }
    private Object[] startFileOutputBuf(String path){
        File file = new File(path);
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter bfw = null;
        try {
            fos = new FileOutputStream(file,true);
            osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
            bfw = new BufferedWriter(osw);
        } catch (IOException e) {
            System.out.println("文件加载错误");
        }
        Object[] objs={(Object)fos,(Object)osw,(Object)bfw};
        return objs;
    }
    private void closeFileInputBuf(Object objs[]){
            try {
                if (((FileInputStream)objs[0]) != null) ((FileInputStream)objs[0]).close();
            } catch (IOException e) {
            }
            try {
                if (((InputStreamReader)objs[1]) != null) ((InputStreamReader)objs[1]).close();
            } catch (IOException e) {
            }
            try {
                if (((BufferedReader)objs[2]) != null) ((BufferedReader) objs[2]).close();
            } catch (IOException e) {
            }
        }
    private void closeFileOutputBuf(Object objs[]){
        try {
            if (((FileOutputStream)objs[0]) != null) ((FileOutputStream)objs[0]).close();
        } catch (IOException e) {
        }
        try {
            if (((OutputStreamWriter)objs[1]) != null) ((OutputStreamWriter)objs[1]).close();
        } catch (IOException e) {
        }
        try {
            if (((BufferedWriter)objs[2]) != null) ((BufferedWriter) objs[2]).close();
        } catch (IOException e) {
        }
    }
    public Map<Integer, vexsinfo> getAllVex() {
        Map<Integer, vexsinfo> vexs = new HashMap<>();
        Object objs[]=startFileInputBuf("resources/vexMessage.txt");
        BufferedReader bfr=(BufferedReader) objs[2];
        try {
            bfr.readLine();
            String line= bfr.readLine();
            while (line!=null&&!line.equals("")) {
                String mess[] = line.split(" ");
                vexsinfo vex = new vexsinfo();
                vex.id = Integer.valueOf(mess[0]);
                vex.name = mess[1];
                vex.realx = Integer.parseInt(mess[2]);
                vex.realy = Integer.parseInt(mess[3]);
                vex.logicx = Integer.parseInt(mess[4]);
                vex.logicy = Integer.parseInt(mess[5]);
                vex.top = Integer.parseInt(mess[6]);
                vex.bottom = Integer.parseInt(mess[7]);
                vex.left = Integer.parseInt(mess[8]);
                vex.right = Integer.parseInt(mess[9]);
                vex.position = Integer.parseInt(mess[10]);
                vex.croded_level = Float.parseFloat(mess[11]);
                vex.drive = Integer.parseInt(mess[12]);
                vexs.put(vex.id, vex);
                line= bfr.readLine();
            }
        }catch (IOException e){
            System.out.println("读取文件失败"+e.getMessage());
        }finally {
            closeFileInputBuf(objs);
        }
        return vexs;
    }
    public List<String> getNames(){
        List<String> nameList=new LinkedList<>();
        Object objs[]=startFileInputBuf("resources/vexMessage.txt");
        BufferedReader bfr=(BufferedReader) objs[2];
        try{
            bfr.readLine();
            String line;
            while((line=bfr.readLine())!=null){
                String mess[]=line.split(" ");
                nameList.add(mess[1]);
            }
        }catch (IOException e){}
        closeFileInputBuf(objs);
        return nameList;
    }
    public vexsinfo getVexByName(String name){
        vexsinfo result=new vexsinfo();
        Map<Integer,vexsinfo> vexs= getAllVex();
        for(vexsinfo v:vexs.values()){
            if(v.name.equals(name)){
                result=v;
                break;
            }
        }
        return result;
    }
    public void setTwoPointDistance(String name,String name1){
        Map<Integer,vexsinfo> vexs= getAllVex();
        int nameV1=0;
        int nameV2=0;
        for(vexsinfo v:vexs.values()){
            if(v.name.equals(name)){
                nameV1=v.id;
            }else if(v.name.equals(name1)){
                nameV2=v.id;
            }
        }
        Object objs[]=startFileOutputBuf("resources/edge.txt");
        BufferedWriter bfw=(BufferedWriter) objs[2];
        try {
            String s=JOptionPane.showInputDialog("请输入"+name+"到"+name1+"的距离:");
            bfw.newLine();
            if(s!=null)bfw.write(nameV1+" "+nameV2+" "+s);
            bfw.flush();
        }catch (IOException e)
        {}finally {
            closeFileOutputBuf(objs);
        }
    }
    public List<String> getAllEdge(){
        List<String> list=new LinkedList<>();
        Object objs[]=startFileInputBuf("resources/edge.txt");
        BufferedReader bfr=(BufferedReader) objs[2];
        try{
            bfr.readLine();
            String line;
            while((line=bfr.readLine())!=null){
                list.add(line);
            }
        }catch (IOException e){}
        closeFileInputBuf(objs);
        return list;
    }
    public List<String> getCrowded() {
        Object[] objs=startFileInputBuf("resources/crowdedPosition.txt");
        List<String> list=new ArrayList<>();
        BufferedReader bfr= (BufferedReader) objs[2];
        String name="";
        while(true){
            try {
                if (!((name=bfr.readLine())!=null)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            list.add(name);
        }
        closeFileInputBuf(objs);
        return list;
    }
    public void addCrowded(String name) {
        Object[] objs = startFileOutputBuf("resources/crowdedPosition.txt");
        BufferedWriter bfr = (BufferedWriter) objs[2];
        try {
            bfr.newLine();
            bfr.write(name);
            bfr.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        closeFileOutputBuf(objs);
    }
    void updateVexDir(int newVexId,String dir[]){
        BufferedReader bfr=null;
        PrintWriter pw=null;
        File file=new File("resources/vexMessage.txt");
        StringBuffer buff=new StringBuffer();//临时容器
        try {
            bfr=new BufferedReader(new FileReader(file));
            String str=bfr.readLine();
            while(str!=null&&!str.equals("")){
                String mess[]=str.split(" ");
                if(mess[0].equals("49")){
                    System.out.println("zhe");
                }
                for(int i=0;i<4;i++){
                    if(dir[i].equals("-1"))continue;
                    if(dir[i].equals(mess[1])){
                        switch (i){
                            case 0:mess[7]=""+newVexId;break;
                            case 1:mess[6]=""+newVexId;break;
                            case 2:mess[9]=""+newVexId;break;
                            case 3:mess[8]=""+newVexId;break;
                        }
                    }
                }
                str="";
                for(int i=0;i<mess.length;i++)str+=mess[i]+" ";
                buff.append(str);
                str=bfr.readLine();
                if(str!=null&&!str.equals(""))buff.append(System.getProperty("line.separator"));
                pw=new PrintWriter(new FileWriter(file),true);
                pw.println(buff);

            }
        }catch (IOException e){e.printStackTrace();}
        finally {
            if(bfr!=null){
                try {
                    bfr.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
                if(pw!=null)pw.close();
            }
        }
    }
    public void addVex(vexsinfo v){
        Object[] objs=startFileOutputBuf("resources/vexMessage.txt");
        BufferedWriter bfw= (BufferedWriter) objs[2];
        try {
            String mess=v.id+" "+v.name+" "+v.realx+" "+v.realy+" "+ v.logicx+" "+v.logicy+" "+v.top+" "+v.bottom+" "+v.left+" "+v.right+" "+v.position+" "+v.croded_level+" "+v.drive+" "+v.introduction;
            bfw.write(mess);
            bfw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        closeFileOutputBuf(objs);
    }

}
